/*      */ package com.zimbra.qa.unittest.prov.ldap;
/*      */ 
/*      */ import com.zimbra.common.account.Key.AccountBy;
/*      */ import com.zimbra.common.account.Key.CacheEntryBy;
/*      */ import com.zimbra.common.account.Key.DistributionListBy;
/*      */ import com.zimbra.common.account.ZAttrProvisioning.DomainType;
/*      */ import com.zimbra.common.service.ServiceException;
/*      */ import com.zimbra.cs.account.Account;
/*      */ import com.zimbra.cs.account.Config;
/*      */ import com.zimbra.cs.account.DistributionList;
/*      */ import com.zimbra.cs.account.Domain;
/*      */ import com.zimbra.cs.account.Entry;
/*      */ import com.zimbra.cs.account.NamedEntry;
/*      */ import com.zimbra.cs.account.Provisioning;
/*      */ import com.zimbra.cs.account.Provisioning.CacheEntry;
/*      */ import com.zimbra.cs.account.SearchDirectoryOptions;
/*      */ import com.zimbra.cs.account.SearchDirectoryOptions.ObjectType;
/*      */ import com.zimbra.cs.account.auth.AuthContext.Protocol;
/*      */ import com.zimbra.cs.account.ldap.LdapHelper;
/*      */ import com.zimbra.cs.account.ldap.LdapProv;
/*      */ import com.zimbra.cs.account.ldap.entry.LdapEntry;
/*      */ import com.zimbra.cs.ldap.LdapUsage;
/*      */ import com.zimbra.cs.ldap.LdapUtil;
/*      */ import com.zimbra.cs.ldap.ZLdapFilterFactory.FilterId;
/*      */ import com.zimbra.qa.QA.Bug;
/*      */ import com.zimbra.soap.admin.type.CacheEntryType;
/*      */ import java.util.HashMap;
/*      */ import java.util.List;
/*      */ import java.util.Map;
/*      */ import java.util.Set;
/*      */ import org.junit.AfterClass;
/*      */ import org.junit.Assert;
/*      */ import org.junit.BeforeClass;
/*      */ import org.junit.Test;
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ public class TestProvAlias
/*      */   extends LdapTest
/*      */ {
/*   51 */   private static String PASSWORD = "test123";
/*      */   
/*      */   private static String BASE_DOMAIN_NAME;
/*      */   
/*      */   private static String LOCAL_DOMAIN_NAME;
/*      */   private static String ALIAS_DOMAIN_NAME;
/*      */   private static String origDefaultDomainName;
/*      */   private static LdapProvTestUtil provUtil;
/*      */   private static Provisioning prov;
/*      */   
/*      */   @BeforeClass
/*      */   public static void init()
/*      */     throws Exception
/*      */   {
/*   65 */     provUtil = new LdapProvTestUtil();
/*   66 */     prov = provUtil.getProv();
/*      */     
/*   68 */     Config config = prov.getConfig();
/*   69 */     origDefaultDomainName = config.getAttr("zimbraDefaultDomainName");
/*      */     
/*   71 */     initTest();
/*      */   }
/*      */   
/*      */   @AfterClass
/*      */   public static void cleanup() throws Exception {
/*   76 */     Config config = prov.getConfig();
/*   77 */     Map<String, Object> attrs = new HashMap();
/*   78 */     attrs.put("zimbraDefaultDomainName", origDefaultDomainName);
/*   79 */     prov.modifyAttrs(config, attrs);
/*      */     
/*   81 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */   private String underscoreToHyphen(String inStr)
/*      */   {
/*   88 */     return inStr.replaceAll("_", "-");
/*      */   }
/*      */   
/*      */   private static void initTest() throws Exception
/*      */   {
/*   93 */     BASE_DOMAIN_NAME = baseDomainName();
/*   94 */     LOCAL_DOMAIN_NAME = "local." + BASE_DOMAIN_NAME;
/*   95 */     ALIAS_DOMAIN_NAME = "alias." + BASE_DOMAIN_NAME;
/*      */     
/*   97 */     Map<String, Object> attrs = new HashMap();
/*      */     
/*      */ 
/*  100 */     attrs.put("zimbraDomainType", ZAttrProvisioning.DomainType.local.name());
/*  101 */     Domain localDomain = provUtil.createDomain(LOCAL_DOMAIN_NAME, attrs);
/*      */     
/*      */ 
/*  104 */     attrs.clear();
/*  105 */     attrs.put("zimbraDomainType", ZAttrProvisioning.DomainType.alias.name());
/*  106 */     attrs.put("zimbraDomainAliasTargetId", localDomain.getId());
/*  107 */     Domain aliasDomain = provUtil.createDomain(ALIAS_DOMAIN_NAME, attrs);
/*      */   }
/*      */   
/*      */   private String getEmail(String localPart, String domainName, String testName) {
/*  111 */     return getLocalPart(localPart, testName) + "@" + domainName;
/*      */   }
/*      */   
/*      */   private String getEmail(String localPart, String domainName) {
/*  115 */     return localPart + "@" + domainName;
/*      */   }
/*      */   
/*      */   private String getLocalPart(String localPart, String testName) {
/*  119 */     return localPart + "-" + testName;
/*      */   }
/*      */   
/*      */   private List<NamedEntry> searchAliasesInDomain(Domain domain) throws ServiceException {
/*  123 */     SearchDirectoryOptions options = new SearchDirectoryOptions();
/*  124 */     options.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.aliases });
/*  125 */     options.setDomain(domain);
/*  126 */     options.setFilterString(ZLdapFilterFactory.FilterId.UNITTEST, null);
/*  127 */     return prov.searchDirectory(options);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */   @Test
/*      */   public void testAliasDomain_Case1()
/*      */     throws Exception
/*      */   {
/*  137 */     String testName = getTestName();
/*      */     
/*  139 */     String acctLocalPart = "account1";
/*  140 */     String acctName = getEmail(acctLocalPart, LOCAL_DOMAIN_NAME, testName);
/*      */     
/*  142 */     String aliasLocalPart = "alias1";
/*  143 */     String aliasName = getEmail(aliasLocalPart, LOCAL_DOMAIN_NAME, testName);
/*      */     
/*  145 */     Account acct = prov.createAccount(acctName, PASSWORD, null);
/*  146 */     prov.addAlias(acct, aliasName);
/*      */     
/*  148 */     String authAs = getEmail(aliasLocalPart, ALIAS_DOMAIN_NAME, testName);
/*  149 */     Account acctGot = prov.get(Key.AccountBy.name, authAs);
/*  150 */     Assert.assertEquals(acct.getId(), acctGot.getId());
/*  151 */     Assert.assertEquals(acct.getName(), acctGot.getName());
/*      */     
/*  153 */     prov.authAccount(acctGot, PASSWORD, AuthContext.Protocol.test);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   @Test
/*      */   public void testAliasDomain_Case2()
/*      */     throws Exception
/*      */   {
/*  164 */     String testName = getTestName();
/*      */     
/*  166 */     String acctLocalPart = "account1";
/*  167 */     String acctName = getEmail(acctLocalPart, LOCAL_DOMAIN_NAME, testName);
/*      */     
/*  169 */     String aliasLocalPart = "alias1";
/*  170 */     String aliasName = getEmail(aliasLocalPart, ALIAS_DOMAIN_NAME, testName);
/*      */     
/*  172 */     Account acct = prov.createAccount(acctName, PASSWORD, null);
/*  173 */     prov.addAlias(acct, aliasName);
/*      */     
/*  175 */     String authAs = getEmail(aliasLocalPart, LOCAL_DOMAIN_NAME, testName);
/*  176 */     Account acctGot = prov.get(Key.AccountBy.name, authAs);
/*  177 */     Assert.assertNull(acctGot);
/*      */     
/*  179 */     authAs = getEmail(acctLocalPart, ALIAS_DOMAIN_NAME, testName);
/*  180 */     acctGot = prov.get(Key.AccountBy.name, authAs);
/*  181 */     Assert.assertEquals(acct.getId(), acctGot.getId());
/*  182 */     Assert.assertEquals(acct.getName(), acctGot.getName());
/*      */     
/*  184 */     prov.authAccount(acctGot, PASSWORD, AuthContext.Protocol.test);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   @Test
/*      */   public void testAliasDomain_Case3()
/*      */     throws Exception
/*      */   {
/*  196 */     String testName = getTestName();
/*      */     
/*  198 */     String acctLocalPart = "account1";
/*  199 */     String acctName = getEmail(acctLocalPart, LOCAL_DOMAIN_NAME, testName);
/*      */     
/*  201 */     String aliasLocalPart = "alias1";
/*  202 */     String aliasName = getEmail(aliasLocalPart, ALIAS_DOMAIN_NAME, testName);
/*      */     
/*  204 */     Config config = prov.getConfig();
/*  205 */     String origDefaltDomainName = config.getAttr("zimbraDefaultDomainName");
/*  206 */     Map<String, Object> attrs = new HashMap();
/*  207 */     attrs.put("zimbraDefaultDomainName", LOCAL_DOMAIN_NAME);
/*  208 */     prov.modifyAttrs(config, attrs);
/*      */     
/*  210 */     Account acct = prov.createAccount(acctName, PASSWORD, null);
/*  211 */     prov.addAlias(acct, aliasName);
/*      */     
/*  213 */     String authAs = getLocalPart(aliasLocalPart, testName);
/*  214 */     Account acctGot = prov.get(Key.AccountBy.name, authAs);
/*  215 */     Assert.assertNull(acctGot);
/*      */     
/*      */ 
/*  218 */     attrs.clear();
/*  219 */     attrs.put("zimbraDefaultDomainName", "");
/*  220 */     prov.modifyAttrs(config, attrs);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   @Test
/*      */   public void testRemoveAlias_entryExist_aliasExist_aliasPointToEntry()
/*      */     throws Exception
/*      */   {
/*  255 */     String testName = getTestName();
/*      */     
/*      */ 
/*  258 */     String domainName = "EE-AE-aliasPointToEntry." + BASE_DOMAIN_NAME;
/*  259 */     domainName = domainName.toLowerCase();
/*  260 */     Map<String, Object> attrs = new HashMap();
/*  261 */     attrs.put("zimbraDomainType", ZAttrProvisioning.DomainType.local.name());
/*  262 */     Domain domain = prov.createDomain(domainName, attrs);
/*      */     
/*      */ 
/*  265 */     String acctName = getEmail("acct-1", domainName);
/*  266 */     Account acct = prov.createAccount(acctName, PASSWORD, new HashMap());
/*      */     
/*      */ 
/*  269 */     String aliasName = getEmail("alias-1", domainName);
/*  270 */     prov.addAlias(acct, aliasName);
/*      */     
/*      */ 
/*  273 */     String dl1Name = getEmail("dl-1", domainName);
/*  274 */     DistributionList dl1 = prov.createDistributionList(dl1Name, new HashMap());
/*      */     
/*  276 */     String dl2Name = getEmail("dl-2", domainName);
/*  277 */     DistributionList dl2 = prov.createDistributionList(dl2Name, new HashMap());
/*      */     
/*      */ 
/*  280 */     prov.addMembers(dl1, new String[] { aliasName });
/*  281 */     prov.addMembers(dl2, new String[] { aliasName });
/*      */     
/*      */ 
/*  284 */     prov.removeAlias(acct, aliasName);
/*      */     
/*      */ 
/*  287 */     prov.reload(acct);
/*  288 */     prov.reload(dl1);
/*  289 */     prov.reload(dl2);
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  294 */     Set<String> values = acct.getMultiAttrSet("mail");
/*  295 */     Assert.assertFalse(values.contains(aliasName));
/*  296 */     values = acct.getMultiAttrSet("zimbraMailAlias");
/*  297 */     Assert.assertFalse(values.contains(aliasName));
/*      */     
/*      */ 
/*  300 */     values = dl1.getMultiAttrSet("zimbraMailForwardingAddress");
/*  301 */     Assert.assertFalse(values.contains(aliasName));
/*  302 */     values = dl2.getMultiAttrSet("zimbraMailForwardingAddress");
/*  303 */     Assert.assertFalse(values.contains(aliasName));
/*      */     
/*      */ 
/*  306 */     List<NamedEntry> aliases = searchAliasesInDomain(domain);
/*  307 */     Assert.assertEquals(aliases.size(), 0L);
/*      */   }
/*      */   
/*      */ 
/*      */   @Test
/*      */   public void testRemoveAlias_entryExist_aliasExist_aliasPointToOtherEntry()
/*      */     throws Exception
/*      */   {
/*  315 */     String testName = getTestName();
/*      */     
/*      */ 
/*  318 */     String domainName = "EE-AE-aliasPointToOtherEntry." + BASE_DOMAIN_NAME;
/*  319 */     domainName = domainName.toLowerCase();
/*  320 */     Map<String, Object> attrs = new HashMap();
/*  321 */     attrs.put("zimbraDomainType", ZAttrProvisioning.DomainType.local.name());
/*  322 */     Domain domain = prov.createDomain(domainName, attrs);
/*      */     
/*      */ 
/*  325 */     String acctName = getEmail("acct-1", domainName);
/*  326 */     Account acct = prov.createAccount(acctName, PASSWORD, new HashMap());
/*      */     
/*      */ 
/*  329 */     String aliasName = getEmail("alias-1", domainName);
/*  330 */     prov.addAlias(acct, aliasName);
/*      */     
/*      */ 
/*  333 */     String dl1Name = getEmail("dl-1", domainName);
/*  334 */     DistributionList dl1 = prov.createDistributionList(dl1Name, new HashMap());
/*      */     
/*  336 */     String dl2Name = getEmail("dl-2", domainName);
/*  337 */     DistributionList dl2 = prov.createDistributionList(dl2Name, new HashMap());
/*      */     
/*      */ 
/*  340 */     prov.addMembers(dl1, new String[] { aliasName });
/*  341 */     prov.addMembers(dl2, new String[] { aliasName });
/*      */     
/*      */ 
/*  344 */     String otherAcctName = getEmail("acct-other", domainName);
/*  345 */     Account otherAcct = prov.createAccount(otherAcctName, PASSWORD, new HashMap());
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  350 */     Map<String, Object> attributes = new HashMap();
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  355 */     attributes.put("zimbraMailAlias", aliasName);
/*  356 */     LdapEntry ldapAccount = (LdapEntry)otherAcct;
/*  357 */     ((LdapProv)prov).getHelper().modifyEntry(ldapAccount.getDN(), attributes, (Entry)ldapAccount, LdapUsage.UNITTEST);
/*      */     
/*      */ 
/*      */ 
/*  361 */     prov.reload(otherAcct);
/*      */     
/*      */ 
/*      */ 
/*  365 */     Set<String> values = otherAcct.getMultiAttrSet("zimbraMailAlias");
/*  366 */     Assert.assertTrue(values.contains(aliasName));
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  372 */     boolean good = false;
/*      */     try {
/*  374 */       prov.removeAlias(otherAcct, aliasName);
/*      */     } catch (ServiceException e) {
/*  376 */       Assert.assertEquals(e.getCode(), "account.NO_SUCH_ALIAS");
/*  377 */       good = true;
/*      */     }
/*  379 */     Assert.assertTrue(good);
/*      */     
/*      */ 
/*  382 */     prov.reload(acct);
/*  383 */     prov.reload(otherAcct);
/*  384 */     prov.reload(dl1);
/*  385 */     prov.reload(dl2);
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  390 */     Set<String> values = acct.getMultiAttrSet("mail");
/*  391 */     Assert.assertTrue(values.contains(aliasName));
/*  392 */     values = acct.getMultiAttrSet("zimbraMailAlias");
/*  393 */     Assert.assertTrue(values.contains(aliasName));
/*      */     
/*      */ 
/*  396 */     values = otherAcct.getMultiAttrSet("mail");
/*  397 */     Assert.assertFalse(values.contains(aliasName));
/*  398 */     values = otherAcct.getMultiAttrSet("zimbraMailAlias");
/*  399 */     Assert.assertFalse(values.contains(aliasName));
/*      */     
/*      */ 
/*  402 */     values = dl1.getMultiAttrSet("zimbraMailForwardingAddress");
/*  403 */     Assert.assertTrue(values.contains(aliasName));
/*  404 */     values = dl2.getMultiAttrSet("zimbraMailForwardingAddress");
/*  405 */     Assert.assertTrue(values.contains(aliasName));
/*      */     
/*      */ 
/*  408 */     List<NamedEntry> aliases = searchAliasesInDomain(domain);
/*  409 */     Assert.assertEquals(aliases.size(), 1L);
/*  410 */     Assert.assertTrue(((NamedEntry)aliases.get(0)).getName().equals(aliasName));
/*      */   }
/*      */   
/*      */ 
/*      */   @Test
/*      */   public void testRemoveAlias_entryExist_aliasExist_aliasPointToNonExistEntry()
/*      */     throws Exception
/*      */   {
/*  418 */     String testName = getTestName();
/*      */     
/*      */ 
/*  421 */     String domainName = "EE-AE-aliasPointToNonExistEntry." + BASE_DOMAIN_NAME;
/*  422 */     domainName = domainName.toLowerCase();
/*  423 */     Map<String, Object> attrs = new HashMap();
/*  424 */     attrs.put("zimbraDomainType", ZAttrProvisioning.DomainType.local.name());
/*  425 */     Domain domain = prov.createDomain(domainName, attrs);
/*      */     
/*      */ 
/*  428 */     String acctName = getEmail("acct-1", domainName);
/*  429 */     Account acct = prov.createAccount(acctName, PASSWORD, new HashMap());
/*      */     
/*      */ 
/*  432 */     String aliasName = getEmail("alias-1", domainName);
/*  433 */     prov.addAlias(acct, aliasName);
/*      */     
/*      */ 
/*  436 */     String dl1Name = getEmail("dl-1", domainName);
/*  437 */     DistributionList dl1 = prov.createDistributionList(dl1Name, new HashMap());
/*      */     
/*  439 */     String dl2Name = getEmail("dl-2", domainName);
/*  440 */     DistributionList dl2 = prov.createDistributionList(dl2Name, new HashMap());
/*      */     
/*      */ 
/*  443 */     prov.addMembers(dl1, new String[] { aliasName });
/*  444 */     prov.addMembers(dl2, new String[] { aliasName });
/*      */     
/*      */ 
/*      */ 
/*  448 */     Map<String, Object> attributes = new HashMap();
/*  449 */     attributes.put("zimbraAliasTargetId", LdapUtil.generateUUID());
/*      */     
/*  451 */     List<NamedEntry> aliases = searchAliasesInDomain(domain);
/*  452 */     Assert.assertEquals(aliases.size(), 1L);
/*  453 */     LdapEntry ldapAlias = (LdapEntry)aliases.get(0);
/*  454 */     ((LdapProv)prov).getHelper().modifyEntry(ldapAlias.getDN(), attributes, (Entry)ldapAlias, LdapUsage.UNITTEST);
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  460 */     boolean good = false;
/*      */     try {
/*  462 */       prov.removeAlias(acct, aliasName);
/*      */     } catch (ServiceException e) {
/*  464 */       Assert.assertEquals(e.getCode(), "account.NO_SUCH_ALIAS");
/*  465 */       good = true;
/*      */     }
/*  467 */     Assert.assertTrue(good);
/*      */     
/*      */ 
/*      */ 
/*  471 */     prov.reload(acct);
/*  472 */     prov.reload(dl1);
/*  473 */     prov.reload(dl2);
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  478 */     Set<String> values = acct.getMultiAttrSet("mail");
/*  479 */     Assert.assertFalse(values.contains(aliasName));
/*  480 */     values = acct.getMultiAttrSet("zimbraMailAlias");
/*  481 */     Assert.assertFalse(values.contains(aliasName));
/*      */     
/*      */ 
/*  484 */     values = dl1.getMultiAttrSet("zimbraMailForwardingAddress");
/*  485 */     Assert.assertFalse(values.contains(aliasName));
/*  486 */     values = dl2.getMultiAttrSet("zimbraMailForwardingAddress");
/*  487 */     Assert.assertFalse(values.contains(aliasName));
/*      */     
/*      */ 
/*  490 */     List<NamedEntry> aliases = searchAliasesInDomain(domain);
/*  491 */     Assert.assertEquals(aliases.size(), 0L);
/*      */   }
/*      */   
/*      */ 
/*      */   @Test
/*      */   public void testRemoveAlias_entryExist_aliasNotExist()
/*      */     throws Exception
/*      */   {
/*  499 */     String testName = getTestName();
/*      */     
/*      */ 
/*  502 */     String domainName = "EE-AN." + BASE_DOMAIN_NAME;
/*  503 */     domainName = domainName.toLowerCase();
/*  504 */     Map<String, Object> attrs = new HashMap();
/*  505 */     attrs.put("zimbraDomainType", ZAttrProvisioning.DomainType.local.name());
/*  506 */     Domain domain = prov.createDomain(domainName, attrs);
/*      */     
/*      */ 
/*  509 */     String acctName = getEmail("acct-1", domainName);
/*  510 */     Account acct = prov.createAccount(acctName, PASSWORD, new HashMap());
/*      */     
/*      */ 
/*  513 */     String aliasName = getEmail("alias-1", domainName);
/*  514 */     prov.addAlias(acct, aliasName);
/*      */     
/*      */ 
/*  517 */     String dl1Name = getEmail("dl-1", domainName);
/*  518 */     DistributionList dl1 = prov.createDistributionList(dl1Name, new HashMap());
/*      */     
/*  520 */     String dl2Name = getEmail("dl-2", domainName);
/*  521 */     DistributionList dl2 = prov.createDistributionList(dl2Name, new HashMap());
/*      */     
/*      */ 
/*  524 */     prov.addMembers(dl1, new String[] { aliasName });
/*  525 */     prov.addMembers(dl2, new String[] { aliasName });
/*      */     
/*      */ 
/*      */ 
/*  529 */     List<NamedEntry> aliases = searchAliasesInDomain(domain);
/*  530 */     Assert.assertEquals(aliases.size(), 1L);
/*  531 */     LdapEntry ldapAlias = (LdapEntry)aliases.get(0);
/*  532 */     String aliasDn = ldapAlias.getDN();
/*  533 */     ((LdapProv)prov).getHelper().deleteEntry(aliasDn, LdapUsage.UNITTEST);
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  538 */     boolean good = false;
/*      */     try {
/*  540 */       prov.removeAlias(acct, aliasName);
/*      */     } catch (ServiceException e) {
/*  542 */       Assert.assertEquals(e.getCode(), "account.NO_SUCH_ALIAS");
/*  543 */       good = true;
/*      */     }
/*  545 */     Assert.assertTrue(good);
/*      */     
/*      */ 
/*      */ 
/*  549 */     prov.reload(acct);
/*  550 */     prov.reload(dl1);
/*  551 */     prov.reload(dl2);
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  556 */     Set<String> values = acct.getMultiAttrSet("mail");
/*  557 */     Assert.assertFalse(values.contains(aliasName));
/*  558 */     values = acct.getMultiAttrSet("zimbraMailAlias");
/*  559 */     Assert.assertFalse(values.contains(aliasName));
/*      */     
/*      */ 
/*  562 */     values = dl1.getMultiAttrSet("zimbraMailForwardingAddress");
/*  563 */     Assert.assertFalse(values.contains(aliasName));
/*  564 */     values = dl2.getMultiAttrSet("zimbraMailForwardingAddress");
/*  565 */     Assert.assertFalse(values.contains(aliasName));
/*      */     
/*      */ 
/*  568 */     List<NamedEntry> aliases = searchAliasesInDomain(domain);
/*  569 */     Assert.assertEquals(aliases.size(), 0L);
/*      */   }
/*      */   
/*      */ 
/*      */   @Test
/*      */   public void testRemoveAlias_entryNotExist_aliasExist_aliasPointToOtherEntry()
/*      */     throws Exception
/*      */   {
/*  577 */     String testName = getTestName();
/*      */     
/*      */ 
/*  580 */     String domainName = "EN-AE-aliasPointToOtherEntry." + BASE_DOMAIN_NAME;
/*  581 */     domainName = domainName.toLowerCase();
/*  582 */     Map<String, Object> attrs = new HashMap();
/*  583 */     attrs.put("zimbraDomainType", ZAttrProvisioning.DomainType.local.name());
/*  584 */     Domain domain = prov.createDomain(domainName, attrs);
/*      */     
/*      */ 
/*  587 */     String acctName = getEmail("acct-1", domainName);
/*  588 */     Account acct = prov.createAccount(acctName, PASSWORD, new HashMap());
/*      */     
/*      */ 
/*  591 */     String aliasName = getEmail("alias-1", domainName);
/*  592 */     prov.addAlias(acct, aliasName);
/*      */     
/*      */ 
/*      */ 
/*  596 */     String dl1Name = getEmail("dl-1", domainName);
/*  597 */     DistributionList dl1 = prov.createDistributionList(dl1Name, new HashMap());
/*      */     
/*  599 */     String dl2Name = getEmail("dl-2", domainName);
/*  600 */     DistributionList dl2 = prov.createDistributionList(dl2Name, new HashMap());
/*      */     
/*      */ 
/*  603 */     prov.addMembers(dl1, new String[] { aliasName });
/*  604 */     prov.addMembers(dl2, new String[] { aliasName });
/*      */     
/*      */ 
/*  607 */     Account nonExistingAcct = null;
/*      */     
/*      */ 
/*      */ 
/*  611 */     prov.removeAlias(nonExistingAcct, aliasName);
/*      */     
/*      */ 
/*  614 */     prov.reload(acct);
/*  615 */     prov.reload(dl1);
/*  616 */     prov.reload(dl2);
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  621 */     Set<String> values = acct.getMultiAttrSet("mail");
/*  622 */     Assert.assertTrue(values.contains(aliasName));
/*  623 */     values = acct.getMultiAttrSet("zimbraMailAlias");
/*  624 */     Assert.assertTrue(values.contains(aliasName));
/*      */     
/*      */ 
/*  627 */     values = dl1.getMultiAttrSet("zimbraMailForwardingAddress");
/*  628 */     Assert.assertTrue(values.contains(aliasName));
/*  629 */     values = dl2.getMultiAttrSet("zimbraMailForwardingAddress");
/*  630 */     Assert.assertTrue(values.contains(aliasName));
/*      */     
/*      */ 
/*  633 */     List<NamedEntry> aliases = searchAliasesInDomain(domain);
/*  634 */     Assert.assertEquals(aliases.size(), 1L);
/*  635 */     Assert.assertTrue(((NamedEntry)aliases.get(0)).getName().equals(aliasName));
/*      */   }
/*      */   
/*      */ 
/*      */   @Test
/*      */   public void testRemoveAlias_entryNotExist_aliasExist_aliasPointToNonExistEntry()
/*      */     throws Exception
/*      */   {
/*  643 */     String testName = getTestName();
/*      */     
/*      */ 
/*  646 */     String domainName = "EN-AE-aliasPointToNonExistEntry." + BASE_DOMAIN_NAME;
/*  647 */     domainName = domainName.toLowerCase();
/*  648 */     Map<String, Object> attrs = new HashMap();
/*  649 */     attrs.put("zimbraDomainType", ZAttrProvisioning.DomainType.local.name());
/*  650 */     Domain domain = prov.createDomain(domainName, attrs);
/*      */     
/*      */ 
/*  653 */     String acctName = getEmail("acct-1", domainName);
/*  654 */     Account acct = prov.createAccount(acctName, PASSWORD, new HashMap());
/*      */     
/*      */ 
/*  657 */     String aliasName = getEmail("alias-1", domainName);
/*  658 */     prov.addAlias(acct, aliasName);
/*      */     
/*      */ 
/*  661 */     String dl1Name = getEmail("dl-1", domainName);
/*  662 */     DistributionList dl1 = prov.createDistributionList(dl1Name, new HashMap());
/*      */     
/*  664 */     String dl2Name = getEmail("dl-2", domainName);
/*  665 */     DistributionList dl2 = prov.createDistributionList(dl2Name, new HashMap());
/*      */     
/*      */ 
/*  668 */     prov.addMembers(dl1, new String[] { aliasName });
/*  669 */     prov.addMembers(dl2, new String[] { aliasName });
/*      */     
/*      */ 
/*      */ 
/*  673 */     Map<String, Object> attributes = new HashMap();
/*  674 */     attributes.put("zimbraAliasTargetId", LdapUtil.generateUUID());
/*      */     
/*  676 */     List<NamedEntry> aliases = searchAliasesInDomain(domain);
/*  677 */     Assert.assertEquals(aliases.size(), 1L);
/*  678 */     LdapEntry ldapAlias = (LdapEntry)aliases.get(0);
/*  679 */     ((LdapProv)prov).getHelper().modifyEntry(ldapAlias.getDN(), attributes, (Entry)ldapAlias, LdapUsage.UNITTEST);
/*      */     
/*      */ 
/*      */ 
/*  683 */     Account nonExistingAcct = null;
/*      */     
/*      */ 
/*      */ 
/*  687 */     prov.removeAlias(nonExistingAcct, aliasName);
/*      */     
/*      */ 
/*      */ 
/*  691 */     prov.reload(acct);
/*  692 */     prov.reload(dl1);
/*  693 */     prov.reload(dl2);
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  700 */     Set<String> values = acct.getMultiAttrSet("mail");
/*  701 */     Assert.assertTrue(values.contains(aliasName));
/*  702 */     values = acct.getMultiAttrSet("zimbraMailAlias");
/*  703 */     Assert.assertTrue(values.contains(aliasName));
/*      */     
/*      */ 
/*  706 */     values = dl1.getMultiAttrSet("zimbraMailForwardingAddress");
/*  707 */     Assert.assertFalse(values.contains(aliasName));
/*  708 */     values = dl2.getMultiAttrSet("zimbraMailForwardingAddress");
/*  709 */     Assert.assertFalse(values.contains(aliasName));
/*      */     
/*      */ 
/*  712 */     List<NamedEntry> aliases = searchAliasesInDomain(domain);
/*  713 */     Assert.assertEquals(aliases.size(), 0L);
/*      */   }
/*      */   
/*      */ 
/*      */   @Test
/*      */   public void testRemoveAlias_entryNotExist_aliasNotExist()
/*      */     throws Exception
/*      */   {
/*  721 */     String testName = getTestName();
/*      */     
/*      */ 
/*  724 */     String domainName = "EN-AN." + BASE_DOMAIN_NAME;
/*  725 */     domainName = domainName.toLowerCase();
/*  726 */     Map<String, Object> attrs = new HashMap();
/*  727 */     attrs.put("zimbraDomainType", ZAttrProvisioning.DomainType.local.name());
/*  728 */     Domain domain = prov.createDomain(domainName, attrs);
/*      */     
/*      */ 
/*  731 */     String acctName = getEmail("acct-1", domainName);
/*  732 */     Account acct = prov.createAccount(acctName, PASSWORD, new HashMap());
/*      */     
/*      */ 
/*  735 */     String aliasName = getEmail("alias-1", domainName);
/*  736 */     prov.addAlias(acct, aliasName);
/*      */     
/*      */ 
/*  739 */     String dl1Name = getEmail("dl-1", domainName);
/*  740 */     DistributionList dl1 = prov.createDistributionList(dl1Name, new HashMap());
/*      */     
/*  742 */     String dl2Name = getEmail("dl-2", domainName);
/*  743 */     DistributionList dl2 = prov.createDistributionList(dl2Name, new HashMap());
/*      */     
/*      */ 
/*  746 */     prov.addMembers(dl1, new String[] { aliasName });
/*  747 */     prov.addMembers(dl2, new String[] { aliasName });
/*      */     
/*      */ 
/*      */ 
/*  751 */     List<NamedEntry> aliases = searchAliasesInDomain(domain);
/*  752 */     Assert.assertEquals(aliases.size(), 1L);
/*  753 */     LdapEntry ldapAlias = (LdapEntry)aliases.get(0);
/*  754 */     String aliasDn = ldapAlias.getDN();
/*  755 */     ((LdapProv)prov).getHelper().deleteEntry(aliasDn, LdapUsage.UNITTEST);
/*      */     
/*      */ 
/*  758 */     Account nonExistingAcct = null;
/*      */     
/*      */ 
/*      */ 
/*  762 */     prov.removeAlias(nonExistingAcct, aliasName);
/*      */     
/*      */ 
/*  765 */     prov.reload(acct);
/*  766 */     prov.reload(dl1);
/*  767 */     prov.reload(dl2);
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  772 */     Set<String> values = acct.getMultiAttrSet("mail");
/*  773 */     Assert.assertTrue(values.contains(aliasName));
/*  774 */     values = acct.getMultiAttrSet("zimbraMailAlias");
/*  775 */     Assert.assertTrue(values.contains(aliasName));
/*      */     
/*      */ 
/*  778 */     values = dl1.getMultiAttrSet("zimbraMailForwardingAddress");
/*  779 */     Assert.assertFalse(values.contains(aliasName));
/*  780 */     values = dl2.getMultiAttrSet("zimbraMailForwardingAddress");
/*  781 */     Assert.assertFalse(values.contains(aliasName));
/*      */     
/*      */ 
/*  784 */     List<NamedEntry> aliases = searchAliasesInDomain(domain);
/*  785 */     Assert.assertEquals(aliases.size(), 0L);
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void testRemoveAlias_aliasNameExistsButIsNotAnAlias() throws Exception {
/*  790 */     String testName = getTestName();
/*      */     
/*      */ 
/*  793 */     String domainName = underscoreToHyphen(testName) + "." + BASE_DOMAIN_NAME;
/*  794 */     domainName = domainName.toLowerCase();
/*  795 */     Map<String, Object> attrs = new HashMap();
/*  796 */     attrs.put("zimbraDomainType", ZAttrProvisioning.DomainType.local.name());
/*  797 */     Domain domain = prov.createDomain(domainName, attrs);
/*      */     
/*      */ 
/*  800 */     String acctName = getEmail("acct-1", domainName);
/*  801 */     Account acct = prov.createAccount(acctName, PASSWORD, new HashMap());
/*      */     
/*      */ 
/*  804 */     String acct2Name = getEmail("acct-2", domainName);
/*  805 */     Account acct2 = prov.createAccount(acct2Name, PASSWORD, new HashMap());
/*  806 */     String acct2Id = acct2.getId();
/*      */     
/*      */ 
/*  809 */     String dlName = getEmail("dl", domainName);
/*  810 */     DistributionList dl = prov.createDistributionList(dlName, new HashMap());
/*  811 */     String dlId = dl.getId();
/*      */     
/*  813 */     boolean good = false;
/*      */     try {
/*  815 */       prov.removeAlias(acct, acct2Name);
/*      */     } catch (ServiceException e) {
/*  817 */       if ("account.NO_SUCH_ALIAS".equals(e.getCode()))
/*  818 */         good = true;
/*      */     }
/*  820 */     Assert.assertTrue(good);
/*      */     
/*      */ 
/*  823 */     prov.flushCache(CacheEntryType.account, new Provisioning.CacheEntry[] { new Provisioning.CacheEntry(Key.CacheEntryBy.id, acct2Id) });
/*  824 */     acct2 = prov.get(Key.AccountBy.id, acct2Id);
/*  825 */     Assert.assertNotNull(acct2);
/*  826 */     Assert.assertEquals(acct2Id, acct2.getId());
/*  827 */     Assert.assertEquals(acct2Name, acct2.getName());
/*      */     try
/*      */     {
/*  830 */       prov.removeAlias(acct, dlName);
/*      */     } catch (ServiceException e) {
/*  832 */       if ("account.NO_SUCH_ALIAS".equals(e.getCode()))
/*  833 */         good = true;
/*      */     }
/*  835 */     Assert.assertTrue(good);
/*      */     
/*      */ 
/*      */ 
/*  839 */     dl = prov.get(Key.DistributionListBy.id, dlId);
/*  840 */     Assert.assertNotNull(dl);
/*  841 */     Assert.assertEquals(dlId, dl.getId());
/*  842 */     Assert.assertEquals(dlName, dl.getName());
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   @Test
/*      */   public void testCreateAlias_aliasExistAndDangling()
/*      */     throws Exception
/*      */   {
/*  853 */     String testName = getTestName();
/*      */     
/*      */ 
/*  856 */     String domainName = underscoreToHyphen(testName) + "." + BASE_DOMAIN_NAME;
/*  857 */     domainName = domainName.toLowerCase();
/*  858 */     Map<String, Object> attrs = new HashMap();
/*  859 */     attrs.put("zimbraDomainType", ZAttrProvisioning.DomainType.local.name());
/*  860 */     Domain domain = prov.createDomain(domainName, attrs);
/*      */     
/*      */ 
/*  863 */     String acctName = getEmail("acct-1", domainName);
/*  864 */     Account acct = prov.createAccount(acctName, PASSWORD, new HashMap());
/*      */     
/*      */ 
/*  867 */     String aliasName = getEmail("alias-1", domainName);
/*  868 */     prov.addAlias(acct, aliasName);
/*      */     
/*      */ 
/*  871 */     List<NamedEntry> aliases = searchAliasesInDomain(domain);
/*  872 */     Assert.assertEquals(aliases.size(), 1L);
/*  873 */     String origZimbraIdOfAlias = ((NamedEntry)aliases.get(0)).getId();
/*      */     
/*      */ 
/*  876 */     String dl1Name = getEmail("dl-1", domainName);
/*  877 */     DistributionList dl1 = prov.createDistributionList(dl1Name, new HashMap());
/*      */     
/*  879 */     String dl2Name = getEmail("dl-2", domainName);
/*  880 */     DistributionList dl2 = prov.createDistributionList(dl2Name, new HashMap());
/*      */     
/*      */ 
/*  883 */     prov.addMembers(dl1, new String[] { aliasName });
/*  884 */     prov.addMembers(dl2, new String[] { aliasName });
/*      */     
/*      */ 
/*      */ 
/*  888 */     LdapEntry ldapAccount = (LdapEntry)acct;
/*  889 */     ((LdapProv)prov).getHelper().deleteEntry(ldapAccount.getDN(), LdapUsage.UNITTEST);
/*      */     
/*      */ 
/*      */ 
/*  893 */     String otherAcctName = getEmail("acct-other", domainName);
/*  894 */     Account otherAcct = prov.createAccount(otherAcctName, PASSWORD, new HashMap());
/*  895 */     prov.addAlias(otherAcct, aliasName);
/*      */     
/*      */ 
/*      */ 
/*  899 */     prov.reload(otherAcct);
/*  900 */     prov.reload(dl1);
/*  901 */     prov.reload(dl2);
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  906 */     Set<String> values = acct.getMultiAttrSet("mail");
/*  907 */     Assert.assertTrue(values.contains(aliasName));
/*  908 */     values = acct.getMultiAttrSet("zimbraMailAlias");
/*  909 */     Assert.assertTrue(values.contains(aliasName));
/*      */     
/*      */ 
/*  912 */     values = dl1.getMultiAttrSet("zimbraMailForwardingAddress");
/*  913 */     Assert.assertFalse(values.contains(aliasName));
/*  914 */     values = dl2.getMultiAttrSet("zimbraMailForwardingAddress");
/*  915 */     Assert.assertFalse(values.contains(aliasName));
/*      */     
/*      */ 
/*  918 */     aliases = searchAliasesInDomain(domain);
/*  919 */     Assert.assertEquals(aliases.size(), 1L);
/*  920 */     Assert.assertFalse(((NamedEntry)aliases.get(0)).getId().equals(origZimbraIdOfAlias));
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void testCreateAlias_aliasNameExistsButIsNotAnAlias() throws Exception {
/*  925 */     String testName = getTestName();
/*      */     
/*      */ 
/*  928 */     String domainName = underscoreToHyphen(testName) + "." + BASE_DOMAIN_NAME;
/*  929 */     domainName = domainName.toLowerCase();
/*  930 */     Map<String, Object> attrs = new HashMap();
/*  931 */     attrs.put("zimbraDomainType", ZAttrProvisioning.DomainType.local.name());
/*  932 */     Domain domain = prov.createDomain(domainName, attrs);
/*      */     
/*      */ 
/*  935 */     String acctName = getEmail("acct-1", domainName);
/*  936 */     Account acct = prov.createAccount(acctName, PASSWORD, new HashMap());
/*      */     
/*      */ 
/*  939 */     String acct2Name = getEmail("acct-2", domainName);
/*  940 */     Account acct2 = prov.createAccount(acct2Name, PASSWORD, new HashMap());
/*      */     
/*      */ 
/*  943 */     String dlName = getEmail("dl", domainName);
/*  944 */     DistributionList dl = prov.createDistributionList(dlName, new HashMap());
/*      */     
/*  946 */     boolean good = false;
/*      */     try {
/*  948 */       prov.addAlias(acct, acct2Name);
/*      */     } catch (ServiceException e) {
/*  950 */       if ("account.ACCOUNT_EXISTS".equals(e.getCode()))
/*  951 */         good = true;
/*      */     }
/*  953 */     Assert.assertTrue(good);
/*      */     try
/*      */     {
/*  956 */       prov.addAlias(acct, dlName);
/*      */     } catch (ServiceException e) {
/*  958 */       if ("account.ACCOUNT_EXISTS".equals(e.getCode()))
/*  959 */         good = true;
/*      */     }
/*  961 */     Assert.assertTrue(good);
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void testCreateAlias_aliasExists() throws Exception {
/*  966 */     String testName = getTestName();
/*      */     
/*      */ 
/*  969 */     String domainName = underscoreToHyphen(testName) + "." + BASE_DOMAIN_NAME;
/*  970 */     domainName = domainName.toLowerCase();
/*  971 */     Map<String, Object> attrs = new HashMap();
/*  972 */     attrs.put("zimbraDomainType", ZAttrProvisioning.DomainType.local.name());
/*  973 */     Domain domain = prov.createDomain(domainName, attrs);
/*      */     
/*      */ 
/*  976 */     String acct1Name = getEmail("acct-1", domainName);
/*  977 */     Account acct1 = prov.createAccount(acct1Name, PASSWORD, new HashMap());
/*      */     
/*      */ 
/*  980 */     String acct2Name = getEmail("acct-2", domainName);
/*  981 */     Account acct2 = prov.createAccount(acct2Name, PASSWORD, new HashMap());
/*      */     
/*      */ 
/*  984 */     String aliasName = getEmail("alias", domainName);
/*  985 */     prov.addAlias(acct1, aliasName);
/*      */     
/*      */ 
/*  988 */     boolean good = false;
/*      */     try {
/*  990 */       prov.addAlias(acct2, aliasName);
/*      */     } catch (ServiceException e) {
/*  992 */       if ("account.ACCOUNT_EXISTS".equals(e.getCode()))
/*  993 */         good = true;
/*      */     }
/*  995 */     Assert.assertTrue(good);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   @Test
/*      */   @QA.Bug(bug={41884})
/*      */   public void testBug41884()
/*      */     throws Exception
/*      */   {
/* 1045 */     String OLD_DOMAIN_NAME = "main." + BASE_DOMAIN_NAME;
/* 1046 */     String NEW_DOMAIN_NAME = "other." + BASE_DOMAIN_NAME;
/* 1047 */     String OLD_LOCALPART = "junk";
/* 1048 */     String NEW_LOCALPART = "phoebe";
/* 1049 */     String OLD_ACCT_NAME = OLD_LOCALPART + "@" + OLD_DOMAIN_NAME;
/* 1050 */     String NEW_ACCT_NAME = NEW_LOCALPART + "@" + NEW_DOMAIN_NAME;
/* 1051 */     String ALIAS_NAME = NEW_LOCALPART + "@" + OLD_DOMAIN_NAME;
/*      */     
/* 1053 */     Domain oldDomain = prov.createDomain(OLD_DOMAIN_NAME, new HashMap());
/* 1054 */     Domain newDomain = prov.createDomain(NEW_DOMAIN_NAME, new HashMap());
/*      */     
/* 1056 */     Account acct = prov.createAccount(OLD_ACCT_NAME, "test123", new HashMap());
/* 1057 */     prov.addAlias(acct, ALIAS_NAME);
/*      */     
/* 1059 */     boolean good = false;
/*      */     try {
/* 1061 */       prov.renameAccount(acct.getId(), NEW_ACCT_NAME);
/*      */     } catch (ServiceException e) {
/* 1063 */       if ("account.ACCOUNT_EXISTS".equals(e.getCode())) {
/* 1064 */         good = true;
/*      */       }
/*      */     }
/* 1067 */     Assert.assertTrue(good);
/*      */   }
/*      */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestProvAlias.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */